//
//  DYMeTableViewController.m
//  BaiSi
//
//  Created by mac on 16/10/14.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYMeTableViewController.h"
#import "DYSettingViewController.h"
#import <AFNetWorking/AFNetworking.h>
#import <MJExtension/MJExtension.h>

#import "DYFooterItem.h"
#import "DYFooterViewCell.h"
#import "DYWebViewController.h"

static NSString * const ID = @"footerCell";
CGFloat margin = 1;
NSInteger colums = 4;
#define itemWH  ((DYScreenW - (colums - 1) * margin) / colums)

@interface DYMeTableViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property(nonatomic,strong) NSMutableArray *footerItems;

@property(nonatomic,weak) UICollectionView *collectionView;


@end

@implementation DYMeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏按钮
    [self setUpNavigationItemButton];
    
    //设置底部视图
    [self setUpFooterView];
    
    //tableView的背景颜色
    self.tableView.backgroundColor = LHColor(215, 215, 215);
    
    //加载底部视图数据
    [self loadFooterViewData];
    
    //设置顶部额外滚动区域
    self.tableView.sectionFooterHeight = 20;
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.contentInset = UIEdgeInsetsMake(-15, 0, 0, 0);
}

#pragma mark - 加载底部视图数据
- (void)loadFooterViewData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"square";
    params[@"c"] = @"topic";
    [manager GET:DYBaseUrl parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        self.footerItems = [DYFooterItem mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
        
        //计算底部视图高度
        NSInteger count = _footerItems.count;
        NSInteger rows = (count - 1) / colums + 1;
        CGFloat CollecitonView_H = rows * itemWH + (rows - 1) * margin;
        _collectionView.xmg_height = CollecitonView_H;
        //让tableView重新自动计算底部视图的高度,并赋值
        self.tableView.tableFooterView = _collectionView;
        //处理缺口
        [self resolveData];
        //刷新表格
        [_collectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        LHLog(@"%@",error);
    }];
    
    
}

#pragma mark - 补全缺口
- (void)resolveData {
    NSInteger count = _footerItems.count;
    NSInteger exer = count % colums;
    if(exer == 0) return;
    for (int i = 0; i < exer; i++) {
        DYFooterItem *item = [[DYFooterItem alloc] init];
        [_footerItems addObject:item];
    }
}

#pragma mark - 设置底部视图
- (void)setUpFooterView {
    //创建流水布局UICollectionViewFlowLayout
    UICollectionViewFlowLayout *layout = ({
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.itemSize = CGSizeMake(itemWH, itemWH);
        layout.minimumLineSpacing = margin;
        layout.minimumInteritemSpacing = margin;
        layout;
    });
    
    
   
    //创建UICollectionView
    UICollectionView *collectionView =({
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        [collectionView registerNib:[UINib nibWithNibName:@"DYFooterViewCell" bundle:nil] forCellWithReuseIdentifier:ID];
        collectionView.backgroundColor = LHColor(215, 215, 215);
        collectionView;
    });
    _collectionView = collectionView;

    //底部视图
    self.tableView.tableFooterView = collectionView;
    
}



#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _footerItems.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    DYFooterViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.item = _footerItems[indexPath.row];
    
    return cell;
    
}

#pragma mark 设置导航栏按钮
- (void)setUpNavigationItemButton {
    //右边
    UIBarButtonItem *setting = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-setting-icon"] highImage:[UIImage imageNamed:@"mine-setting-icon-click"] target:self action:@selector(setting)];
    
    UIBarButtonItem *night = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-moon-icon"] selImage:[UIImage imageNamed:@"mine-sun-icon"] target:self action:@selector(night:)];
    
    self.navigationItem.rightBarButtonItems = @[setting, night];
    
    //中间
    self.navigationItem.title = @"我的";
    
//    //设置字体颜色,大小
//    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
//    attr[NSForegroundColorAttributeName] = [UIColor randomColor];
//    attr[NSFontAttributeName] = [UIFont systemFontOfSize:20];
//    self.navigationController.navigationBar.titleTextAttributes = attr;
}

#pragma mark - 夜间模式
- (void)night:(UIButton *)button {
    button.selected = !button.selected;
    LHLog(@"点击夜间模式");
}

#pragma mark - 进入设置界面
- (void)setting {
    LHLog(@"点击设置");
    
    DYSettingViewController *setVc = [[DYSettingViewController alloc] init];
    [self.navigationController pushViewController:setVc animated:YES];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //判断是否有网页
    DYFooterItem *item = _footerItems[indexPath.row];
    if (![item.url hasPrefix:@"http"]) return;
    
    NSURL *url = [NSURL URLWithString:item.url];
    
    //跳转
    DYWebViewController *webVc = [[DYWebViewController alloc] init];
    webVc.url = url;
    [self.navigationController pushViewController:webVc animated:YES];
    
}



@end
